package com.bicon.botu.mall.base.controller;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.bicon.botu.core.base.common.Constants;
import com.bicon.botu.core.base.common.controller.BaseController;
import com.bicon.botu.core.base.common.entity.BaseResult;
import com.bicon.botu.mall.base.common.ValidationUtil;
import com.bicon.botu.mall.base.dto.FreightDispatchAreaDTO;
import com.bicon.botu.mall.base.dto.FreightTemplateDTO;
import com.bicon.botu.mall.base.entity.FreightDispatchArea;
import com.bicon.botu.mall.base.entity.FreightTemplate;
import com.bicon.botu.mall.base.service.IFreightTemplateService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 运费模板 前端控制器
 * </p>
 *
 * @author liushujie
 * @since 2018-05-11
 */
@RestController
@RequestMapping("/V3.0.0/freight-template")
public class FreightTemplateController extends BaseController {

    @Autowired
    IFreightTemplateService iFreightTemplateService;

    /**
     * 分页查询运费模板列表
     *
     * @param pageNo   当前页
     * @param pageSize 每页显示数
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public BaseResult list(@RequestParam(value = "pageNo") int pageNo,
                           @RequestParam(value = "pageSize") int pageSize) {
        Page<FreightTemplate> page = new FreightTemplate().selectPage(new Page<>(pageNo, pageSize),
                new EntityWrapper<FreightTemplate>().eq("logic_delete", Constants.DEL_LOGIC_NORMAL).orderBy("sort", false));
        return sendResult200(page);
    }

    /**
     * 启用/禁用 运费模板
     *
     * @param id     主键
     * @param status 状态是否启用（0:启用，1:未启用）
     * @return
     */
    @RequestMapping(value = "/status",method = RequestMethod.POST)
    public BaseResult status(@RequestParam(value = "id") String id, @RequestParam(value = "status") Integer status) {
        FreightTemplate freightTemplate = new FreightTemplate();
        freightTemplate.setId(id);
        freightTemplate.setEnable(status);
        freightTemplate.preUpdate();
        return freightTemplate.updateById() ? sendResult200() : sendResult500();
    }

    /**
     * 删除运费模板
     * @param id 主键
     * @return
     */
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public BaseResult delete(@RequestParam(value = "id") String id) {
        FreightTemplate freightTemplate = new FreightTemplate();
        freightTemplate.setId(id);
        freightTemplate.preDelete();
        return freightTemplate.updateById() ? sendResult200() : sendResult500();
    }

    /**
     * 根据模板id运费模板详情
     * @param id 运费模板ID
     * @return 模板对象
     */
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public BaseResult detail(@RequestParam("id") String id) {
        FreightTemplateDTO freightTemplateDTO = new FreightTemplateDTO();
        FreightTemplate freightTemplate = new FreightTemplate();
        freightTemplate.setId(id);
        freightTemplate = freightTemplate.selectById();
        if(ValidationUtil.isEmpty(freightTemplate)){
            return new BaseResult(Constants.RESPONSE_CODE_602, Constants.RESPONSE_MESSAGE_602);
        }
        //根据运费模板id 获取模板区域list
        List<FreightDispatchArea> freightDispatchAreaList = new  FreightDispatchArea().selectList(new EntityWrapper<>().eq("freight_template_id", id)
                .eq("logic_delete", Constants.DEL_LOGIC_NORMAL));
        if(ValidationUtil.isEmpty(freightDispatchAreaList)) {
            return  new BaseResult(Constants.RESPONSE_CODE_602, Constants.RESPONSE_MESSAGE_602);
        }
        BeanUtils.copyProperties(freightTemplate,freightTemplateDTO);
        List<FreightDispatchAreaDTO> freightDispatchAreaDTOList = new ArrayList<>();
        for(FreightDispatchArea freightDispatchArea:freightDispatchAreaList){
            FreightDispatchAreaDTO freightDispatchAreaDTO = new FreightDispatchAreaDTO();
            BeanUtils.copyProperties(freightDispatchArea,freightDispatchAreaDTO);
            freightDispatchAreaDTOList.add(freightDispatchAreaDTO);
        }
        freightTemplateDTO.setFreightDispatchAreaList(freightDispatchAreaDTOList);
        return new BaseResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200, freightTemplateDTO);
    }

    /**
     * 新增运费模板模板
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public BaseResult insert(@RequestParam (value = "freightTemplateParam") String freightTemplateParam) {
        Type type = new TypeReference<FreightTemplateDTO>() {}.getType();
        FreightTemplateDTO freightTemplateDTO = JSONObject.parseObject(freightTemplateParam,type);
        if(ValidationUtil.isEmpty(freightTemplateDTO)){
            return new BaseResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400);
        }
        //检查重复
        FreightTemplate freightTemplate = new FreightTemplate().selectOne(new EntityWrapper<FreightTemplate>().eq("name", freightTemplateDTO.getName())
                .eq("logic_delete", Constants.DEL_LOGIC_NORMAL));
        if (freightTemplate != null) {
            return new BaseResult(Constants.RESPONSE_CODE_601, Constants.RESPONSE_MESSAGE_601);
        }
        return iFreightTemplateService.insert(freightTemplateDTO) ? sendResult200() : sendResult500();
    }

    /**
     * 修改运费模板模板
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public BaseResult update(@RequestParam (value = "freightTemplateParam") String freightTemplateParam) {
        Type type = new TypeReference<FreightTemplateDTO>() {}.getType();
        FreightTemplateDTO freightTemplateDTO = JSONObject.parseObject(freightTemplateParam,type);
        if(ValidationUtil.isEmpty(freightTemplateDTO)){
            return new BaseResult(Constants.RESPONSE_CODE_400, Constants.RESPONSE_MESSAGE_400);
        }
        if(ValidationUtil.isEmpty(freightTemplateDTO.getName())){
            FreightTemplate freightTemplate = new FreightTemplate().selectOne(new EntityWrapper<FreightTemplate>().notIn("id",freightTemplateDTO.getId()).eq("name", freightTemplateDTO.getName())
                    .eq("logic_delete", Constants.DEL_LOGIC_NORMAL));
            //检查重复
            if (freightTemplate != null && !ValidationUtil.equals(freightTemplateDTO.getId(),freightTemplate.getId())) {
                return new BaseResult(Constants.RESPONSE_CODE_601, Constants.RESPONSE_MESSAGE_601);
            }
        }
        return iFreightTemplateService.update(freightTemplateDTO) ? sendResult200() : sendResult500();
    }

    /**
     * 获取已启用的运费模板
     * @return
     */
    @RequestMapping(value = "/enabled-list", method = RequestMethod.GET)
    public BaseResult enabled() {
        List<FreightTemplate> freightTemplateList = new FreightTemplate().selectList(new EntityWrapper<FreightTemplate>().eq("logic_delete", Constants.DEL_LOGIC_NORMAL).
                eq("enable", 0).orderBy("sort", false));
        return new BaseResult(Constants.RESPONSE_CODE_200, Constants.RESPONSE_MESSAGE_200, freightTemplateList);
    }

}
